package com.director.core;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * A direct context bind to a single request thread direct invocation.
 *
 * Author: Simone Ricciardi
 * Date: 2-lug-2010
 * Time: 14.34.42
 */
public class DirectContext {

   private static ThreadLocal<DirectContext> CONTEXT = new ThreadLocal<DirectContext>();

   private DirectConfiguration configuration;
   private HttpServletRequest request;
   private List<DirectEvent> events = new ArrayList<DirectEvent>();

   private DirectContext(HttpServletRequest request, DirectConfiguration configuration) {
      this.request = request;
      this.configuration = configuration;
   }

   /**
    * Returns the DirectRequestContext specific to the current thread.
    *
    * @return the DirectRequestContext for the current thread, is never <tt>null</tt>.
    */
   public static DirectContext get() {
      return CONTEXT.get();
   }

   /**
    *
    * @param request
    * @param configuration
    *
    */
   static void init(HttpServletRequest request, DirectConfiguration configuration) {
      CONTEXT.set(new DirectContext(request, configuration));
   }

   /**
    *
    */
   static void dispose() {
      CONTEXT.set(null);
   }

   /**
    *
    * @return
    */
   public HttpServletRequest getRequest() {
      return request;
   }

   /**
    *
    * @return
    */
   public DirectConfiguration getConfiguration() {
      return configuration;
   }

   /**
    *
    * @return
    */
   public List<DirectEvent> getEvents() {
      return events;
   }

   /**
    *
    * @param event
    */
   public void pushEvent(DirectEvent event) {
      this.events.add(event);
   }

}
